class BoyerMooreSearch {
	public static void main(String[] args) {
		boolean result = boyerMooreSearch(args[0], args[1]);
		System.out.println("Answer is: " + result);
	}

	private static boolean boyerMooreSearch(String text, String pattern) {
		int pLen = pattern.length() - 1;
		int tLen = text.length() - 1;

		if(pLen < 0 || tLen < 0 || tLen < pLen){
			return false;
		}

		for(int j = pLen; j <= tLen;){
			int i = j;
			int k = pLen;

			//i represents index of char in text which mismatched with pattern char
			for(;k >=0 && text.charAt(i) == pattern.charAt(k); --i, --k);

			if(k < 0){
				System.out.println("Matched index is: " + (i+1));
				return true; // pattern matached
			}

			//Crux of Boyer Moore Algorithm
			if(pattern.lastIndexOf(text.charAt(i)) < 0 || (pLen - pattern.lastIndexOf(text.charAt(i))) <= 0) { //pattern should always move right to align, never towards left
				j = j + pLen + 1;
			}else{
				j = j + pLen - pattern.lastIndexOf(text.charAt(i)); // align the matching character indices
			}
		}

		return false;
	}
}
